package org.edu.modules.scheduleCommon.dto;

import com.alibaba.excel.write.handler.SheetWriteHandler;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteWorkbookHolder;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;

public class CustomHeaderHandler implements SheetWriteHandler {

    @Override
    public void beforeSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {

    }

    @Override
    public void afterSheetCreate(WriteWorkbookHolder writeWorkbookHolder, WriteSheetHolder writeSheetHolder) {
        Sheet sheet = writeSheetHolder.getSheet();
        Workbook workbook = writeWorkbookHolder.getWorkbook();

        // ==== 第一行表头：合并周一到周五 ====
        Row headerRow1 = sheet.createRow(0);
        Cell mondayToFridayCell = headerRow1.createCell(0);
        mondayToFridayCell.setCellValue("周一至周五");
        sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 4));

        // ==== 第二行表头：每天9节课 ====
        Row headerRow2 = sheet.createRow(1);
        int columnIndex = 0;
        String[] weekDays = {"周一", "周二", "周三", "周四", "周五"};
        for (String weekDay : weekDays) {
            for (int period = 1; period <= 9; period++) {
                Cell cell = headerRow2.createCell(columnIndex);
                cell.setCellValue("第" + period + "节");
                columnIndex++;
            }
        }

        // ==== 学生姓名列 ====
        Cell studentNameCell = headerRow2.createCell(columnIndex);
        studentNameCell.setCellValue("学生姓名");

        // ==== 设置表头样式（加粗居中） ====
        CellStyle headerStyle = workbook.createCellStyle();
        Font font = workbook.createFont();
        //font.setBold(true);
        headerStyle.setFont(font);
        //headerStyle.setAlignment(HorizontalAlignment.CENTER);
        //headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);

        for (Cell cell : headerRow1) cell.setCellStyle(headerStyle);
        for (Cell cell : headerRow2) cell.setCellStyle(headerStyle);
    }
}


